<?php
/*
 * Widgets template
 */

/*
 * Recent work widget 
 */
class VNWP_Recent_Work extends WP_Widget {
	function VNWP_Recent_Work() {
		parent::WP_Widget('VNWP_Recent_Work', 'Recent Work VNWP',
				array(
					'description' => 'Recent Work Widget VNWP'
				));
	}
	
	function widget($args, $instance) {
		extract($args);
		$title = empty($instance['title']) ? __('Recent Work', DOMAIN_TRANS) : $instance['title'];
		$category = isset($instance['category']) ? $instance['category'] : 1;
		echo $before_widget;
		echo $before_title . $title . $after_title;
		$args = array(
			'cat' => $category,
			'showposts' => 1,
		);
		global $wp_query;
		$temp = $wp_query;
		$wp_query = new WP_Query($args);
		if ($wp_query->have_posts()) {
			$width = 256;
			$height = 150;
			while ($wp_query->have_posts()) {
				$wp_query->the_post();
				echo '<div id="vnwp_recent_work">' . "\n";
				echo '<a title="' . get_the_title() . '" href="'
						. get_permalink() . '">';
				if (has_post_thumbnail()) {
						$thumb = wp_get_attachment_image_src(
								get_post_thumbnail_id(), 'full');
						echo get_timthumb_img_src($thumb[0], $width, $height,
								get_the_title(), 'img-shadow');
					}
				echo '</a>';
				echo '<p style="text-align:center;">';
				the_title();
				echo '</p>';
				echo '</div><!--VNWP_Recent_Work-->' . "\n";
			}
		}
		$wp_query = null;
		$wp_query = $temp;
		wp_reset_query();
		echo $after_widget;
	}
	
	function form($instance) {
		$instance = wp_parse_args((array) $instance,
				array(
					'title' => '',
					'category' => 1
				));
		$title = strip_tags($instance['title']);
		$category = $instance['category'];
		echo '<p>';
		echo '<label for="' . $this->get_field_id('title') . '">';
		_e('Title:', DOMAIN_TRANS);
		echo '</label>';
		echo '<input class="widefat" id="' . $this->get_field_id('title')
				. '"name="' . $this->get_field_name('title')
				. '" type="text" 
				value="' . esc_attr($title) . '" />';
		echo '</p>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('category') . '">';
		_e('Category:', DOMAIN_TRANS);
		echo '</label>';
		wp_dropdown_categories(
				array(
					'id' => $this->get_field_id('category'),
					'selected' => $instance['category'],
					'name' => $this->get_field_name('category'),
					'class' => 'widefat'
				));
		echo '</p>';
	}
	
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['category'] = $new_instance['category'];
		return $instance;
	}
}//end class VNWP_Recent_Work
register_widget('VNWP_Recent_Work');

/*
 * Free themes
 */
class VNWP_Posts_Widget extends WP_Widget {
	function VNWP_Posts_Widget() {
		parent::WP_Widget('VNWP_Posts_Widget', 'Posts Widget VNWP',
				array(
					'description' => 'Porfolio Widget VNWP'
				));
	}
	
	function widget($args, $instance) {
		extract($args);
		$title = empty($instance['title']) ? __('Porfolio', DOMAIN_TRANS) : $instance['title'];
		$category = isset($instance['category']) ? $instance['category'] : 1;
		$showposts = isset($instance['showposts']) ? $instance['showposts'] : 4;
		$showas = isset($instance['showas']) ? $instance['showas'] : 'list';
		$width = $instance['width'];
		$showtitle = isset($instance['showtitle']) ? true : false;
		$showthumb = isset($instance['showthumb']) ? true : false;
		if (!is_numeric($width))
			$width = 150;
		$height = $instance['height'];
		if (!is_numeric($height))
			$height = 150;
		echo $before_widget;
		echo $before_title . $title . $after_title;
		$args = array(
			'cat' => $category,
			'showposts' => $showposts,
		);
		global $wp_query;
		$temp = $wp_query;
		$wp_query = new WP_Query($args);
		if ($wp_query->have_posts()) {
			echo '<ul class="portfolio-list ' . $showas . '">' . "\n";
			$i = 1;
			while ($wp_query->have_posts()) {
				$wp_query->the_post();
				if ($i % 2 == 0)
					echo '<li class="even">';
				else
					echo '<li>';
				$i++;
				if ($showthumb) {
					echo '<a title="' . get_the_title() . '" href="'
							. get_permalink() . '">';
					if (has_post_thumbnail()) {
						$thumb = wp_get_attachment_image_src(
								get_post_thumbnail_id(), 'full');
						echo get_timthumb_img_src($thumb[0], $width, $height,
								get_the_title(), 'img-shadow');
					}
					echo '</a>';
				}
				if ($showtitle)
					echo '<a title="' . get_the_title() . '" href="'
							. get_permalink() . '">' . get_the_title() . '</a>';
				
				echo '</li>';
			}
			echo '</ul><!--.portfolio-list-->' . "\n";
		}
		$wp_query = null;
		$wp_query = $temp;
		wp_reset_query();
		echo $after_widget;
	}
	
	function form($instance) {
		$instance = wp_parse_args((array) $instance,
				array(
					'title' => '',
					'category' => 1,
					'showposts' => 4,
					'showas' => 'list',
					'width' => 150,
					'height' => 150,
					'showtitle' => true,
					'showthumb' => true
				));
		$title = strip_tags($instance['title']);
		$category = $instance['category'];
		$showposts = $instance['showposts'];
		$showas = $instance['showas'];
		$width = strip_tags($instance['width']);
		$height = strip_tags($instance['height']);
		$showtitle = $instance['showtitle'];
		$showthumb = $instance['showthumb'];
		echo '<p>';
		echo '<label for="' . $this->get_field_id('title') . '">';
		_e('Title:', DOMAIN_TRANS);
		echo '</label>';
		echo '<input class="widefat" id="' . $this->get_field_id('title')
				. '"name="' . $this->get_field_name('title')
				. '" type="text" 
				value="' . esc_attr($title) . '" />';
		echo '</p>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('category') . '">';
		_e('Category:', DOMAIN_TRANS);
		echo '</label>';
		wp_dropdown_categories(
				array(
					'id' => $this->get_field_id('category'),
					'selected' => $instance['category'],
					'name' => $this->get_field_name('category'),
					'class' => 'widefat'
				));
		echo '</p>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('showposts') . '">';
		_e('Show posts:', DOMAIN_TRANS);
		echo '</label>';
		echo '<input id="' . $this->get_field_id('showposts') . '"name="'
				. $this->get_field_name('showposts')
				. '" type="text" size="3" 
				value="' . esc_attr($showposts) . '" />';
		echo '</p>';
		echo '<p>';
		echo '<label>';
		_e('Show as: ', DOMAIN_TRANS);
		echo '</label>';
		echo '<input id="' . $this->get_field_id('showas_list') . '" name="'
				. $this->get_field_name('showas') . '" type="radio" ';
		if ($showas == 'list')
			echo 'checked="checked"';
		echo ' value="list"/>';
		echo '<label for="' . $this->get_field_id('showas_list')
				. '"> list&nbsp;&nbsp;&nbsp;&nbsp;</label>';
		echo '<input id="' . $this->get_field_id('showas_grid') . '" name="'
				. $this->get_field_name('showas') . '" type="radio" ';
		if ($showas == 'grid')
			echo 'checked="checked"';
		echo ' value="grid"/>';
		echo '<label for="' . $this->get_field_id('showas_grid')
				. '"> grid </label>';
		echo '</p>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('showtitle') . '">';
		$checked = '';
		if ($showtitle)
			$checked = 'checked="checked"';
		echo '<input id="' . $this->get_field_id('showtitle')
				. '" type="checkbox" name="'
				. $this->get_field_name('showtitle') . '" ' . $checked . '/>';
		echo ' ' . __('Show title', DOMAIN_TRANS);
		echo '</label>';
		echo '</p>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('showthumb') . '">';
		$checked = '';
		if ($showthumb)
			$checked = 'checked="checked"';
		echo '<input id="' . $this->get_field_id('showthumb')
				. '" type="checkbox" name="'
				. $this->get_field_name('showthumb') . '" ' . $checked . '/>';
		echo ' ' . __('Show thumbnail', DOMAIN_TRANS);
		echo '</label>';
		echo '</p>';
		echo '<label>';
		_e('Size of thumbnail:', DOMAIN_TRANS);
		echo '</label>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('width') . '">';
		_e('Width: ', DOMAIN_TRANS);
		echo '</label>';
		echo '<input id="' . $this->get_field_id('width') . '"name="'
				. $this->get_field_name('width')
				. '" type="text" size="3"
				value="' . esc_attr($width) . '" />';
		echo '<label> px</label>';
		echo '</p>';
		echo '<p>';
		echo '<label for="' . $this->get_field_id('height') . '">';
		_e('Height: ', DOMAIN_TRANS);
		echo '</label>';
		echo '<input id="' . $this->get_field_id('height') . '"name="'
				. $this->get_field_name('height')
				. '" type="text" size="3"
				value="' . esc_attr($height) . '" />';
		echo '<label> px</label>';
		echo '</p>';
	}
	
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['category'] = $new_instance['category'];
		$instance['showposts'] = $new_instance['showposts'];
		$instance['showas'] = $new_instance['showas'];
		$instance['width'] = strip_tags($new_instance['width']);
		$instance['height'] = strip_tags($new_instance['height']);
		$instance['showtitle'] = $new_instance['showtitle'];
		$instance['showthumb'] = $new_instance['showthumb'];
		return $instance;
	}
}//end class VNWP_Posts_Widget
register_widget('VNWP_Posts_Widget');

/*
 * Related Posts base on Tags or same category
 */
class VNWP_Related_Posts extends WP_Widget {
	
	/*
	 * constructor
	 */
	function VNWP_Related_Posts() {
		parent::__construct('VNWP_Related_Posts', 'Related Posts VNWP',
				array(
					'description' => 'Show related posts base on tags or category',
					'classname' => 'wg-vnwp-related-posts'
				));
	}
	
	/*
	 * widget content
	 */
	function widget($args, $instance) {
		global $post;
		extract($args);
		$instance = wp_parse_args((array) $instance,
				array(
					'title' => __('Related Posts', DOMAIN_TRANS)
				));
		echo $before_widget;
		echo $before_title . $instance['title'] . $after_title;
		//for use in the loop, list 5 post titles related to first tag on current post
		$tags = wp_get_post_tags($post->ID);
		if ($tags) {
			foreach ($tags as $tag)
				$tag_ids[] = $tag->term_id;
			$args = array(
				'tag__in' => $tag_ids,
				'post__not_in' => array(
					$post->ID
				),
				'showposts' => 3,
				'ignore_sticky_posts' => 1
			);
		} else {
			$cats = wp_get_post_categories($post->ID);
			$args = array(
				'category__in' => $cats,
				'post__not_in' => array(
					$post->ID
				),
				'showposts' => 3,
				'ignore_sticky_posts' => 1
			);
		}
		global $wp_query;
		$temp = $wp_query;
		$wp_query = new WP_Query($args);
		if (have_posts()) {
			$width = 190;
			$height = 115;
			echo '<ul class="related-posts">';
			while (have_posts()) {
				the_post();
				echo '<li>';
				echo '<a href="' . get_permalink() . '" title="'
						. get_the_title() . '">';
				if (has_post_thumbnail()) {
					$thumb = wp_get_attachment_image_src(
							get_post_thumbnail_id(), 'full');
					echo get_timthumb_img_src($thumb[0], $width, $height,
							get_the_title(), 'img-shadow');
				}
				echo '</a>';
				echo '</li>';
			}
			echo '</ul><!--#related-posts--->';
		}
		$wp_query = NULL;
		$wp_query = $temp;
		wp_reset_query();
		echo $after_widget;
	}
}
register_widget('VNWP_Related_Posts');

/*
 * blog widget: popular - latest - comments - tags - archive
 */
class VNWP_Blog_Widget extends WP_Widget {
	
	/*
	 * constructor
	 */
	function VNWP_Blog_Widget() {
		parent::__construct('VNWP_Blog_Widget', 'Blog Widget VNWP',
				array(
					'description' => 'Show tabs: popular - latest - comments - tags - archive',
					'classname' => 'wg-vnwp-blog'
				));
	}
	
	/*
	 * widget content
	 */
	function widget($args, $instance) {
		global $post;
		extract($args);
		$instance = wp_parse_args((array) $instance,
				array(
					'title' => ''
				));
		echo $before_widget;
		?>
		<ul class="tabs-blog">
			<li class="tab-active"><a href="#popular_tab">Popular</a></li>
			<li><a href="#latest_tab">Latest</a></li>
			<li><a href="#comments_tab">Comments</a></li>
			<li><a href="#tags_tab">Tags</a></li>
		</ul><!-- .tabs-blog -->
		<div class="tabs-blog-contents clear">
			<ul id="popular_tab" class="blog-list">
			<?php
				$post_args = array(
					'showposts'	=>	5,
					'orderby'	=>	'comment_count'					
				);
				$popular = new WP_Query($post_args);
				if($popular->have_posts()){
					$width = $height = 55;					
					while($popular->have_posts()){
						$popular->the_post();
						echo '<li>';
						$link = '<a title="' . get_the_title() . '" href="' . get_permalink() . '">';
						echo $link;
						if (has_post_thumbnail())
							$thumb = wp_get_attachment_image_src(get_post_thumbnail_id());
							echo get_timthumb_img_src($thumb[0], $width, $height,
									get_the_title(), 'latest-post-thumb');
						echo '</a>';
						echo str_replace('<a', '<a class="post-link" ', $link);
						the_title();
						echo '</a>';
						echo '<span class="meta">' . get_the_date() . '</span>';
						echo '</li>';
					}
				}					
				$latest = null;
				wp_reset_query(); 
			?>
			</ul><!-- #popular_tab -->
			<ul id="latest_tab" class="blog-list hide">
			<?php
				$post_args = array(
					'showposts'	=>	5					
				);
				$latest = new WP_Query($post_args);
				if($latest->have_posts()){
					$width = $height = 55;					
					while($latest->have_posts()){
						$latest->the_post();
						echo '<li>';
						$link = '<a title="' . get_the_title() . '" href="' . get_permalink() . '">';
						echo $link;
						if (has_post_thumbnail())
							$thumb = wp_get_attachment_image_src(get_post_thumbnail_id());
							echo get_timthumb_img_src($thumb[0], $width, $height,
									get_the_title(), 'latest-post-thumb');
						echo '</a>';
						echo str_replace('<a', '<a class="post-link" ', $link);
						the_title();
						echo '</a>';
						echo '<span class="meta">' . get_the_date() . '</span>';
						echo '</li>';
					}
				}					
				$latest = null;
				wp_reset_query(); 
			?>
			</ul><!-- #latest_tab -->
			<ul id="comments_tab" class="blog-list hide">
			<?php
				$cm_args = array(
					'number'	=> 5
				);
				$comments = get_comments($cm_args);
				foreach ($comments as $cm){
					echo '<li>';
					$link = '';
					if($cm->comment_author_url)
						$link = '<a title="'. $cm->comment_author_url .'" href="'. $cm->comment_author_url .'">';
					echo $link;
					echo get_avatar($cm->comment_author_email, '50', THEME_URI . 'images/default_avatar.png');
					if($cm->comment_author_url)
						echo '</a>';
					echo '<a class="post-link" title="'. $cm->comment_author .'" href="'. get_permalink($cm->comment_post_ID) .'#comment-'. $cm->comment_ID .'">';
					echo $cm->comment_author;
					echo '</a>';
					echo '<p>';
					$cm_arr = explode(' ', $cm->comment_content, 15);
					if(count($cm_arr) >= 15){
						for($i = 0; $i < count($cm_arr) - 1; $i++)
							echo $cm_arr[$i] . ' ';
						echo '...';
					}else
						echo $cm->comment_content;
					echo '</p>';
					echo '</li>';
				}
			?>
			</ul><!-- #comment_tab -->
			<ul id="tags_tab" class="blog-list hide">
				<li>
				<?php
					wp_tag_cloud(); 
				?>
				</li>
			</ul><!-- #tags_tab -->
		</div><!-- .tabs-blog-contents -->
		<script type="text/javascript">
			jQuery('ul.tabs-blog li a').click(function(){
				$this = jQuery(this);
				$parent = $this.parent('li');
				if($parent.hasClass('tab-active'))
					return false;
				else{
					jQuery('.tabs-blog-contents ul').removeClass('hide');
					jQuery('.tabs-blog-contents ul').hide();
					jQuery($this.attr('href')).fadeIn(400, function(){
						jQuery('.tabs-blog li').removeClass('tab-active');
						$parent.addClass('tab-active');
					});
					return false;
				}
			});
		</script>
		<?php 
		echo $after_widget;
	}
}
register_widget('VNWP_Blog_Widget');